package com.ruoyi.project.cms.knowledgemanagement.controller;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.knowledgemanagement.domain.Knowledge;
import com.ruoyi.project.cms.knowledgemanagement.domain.KnowledgeBenchmarking;
import com.ruoyi.project.cms.knowledgemanagement.service.IKnowledgeBenchmarkingService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/knowledgemanagement/knowledgeBenchmarking")
public class KnowledgeBenchmarkingController extends BaseController {

    @Autowired
    private IKnowledgeBenchmarkingService iKnowledgeBenchmarkingService;

    String prefix="/cms/knowledgemanagement";

    @GetMapping("")
    public String knoledge(){
        return prefix+"/benchmarking";
    }


        /*
        * 查询
        * */

        @PostMapping("/list")
        @ResponseBody
    public TableDataInfo list(KnowledgeBenchmarking knowledgeBenchmarking){
        startPage();
        List<KnowledgeBenchmarking>  list  = iKnowledgeBenchmarkingService.findByName(knowledgeBenchmarking);
        return getDataTable(list);
    }



    /*
    * 删除单个角色和批量删除操作
    * */
    @RequiresPermissions("cms:knowledgemanagement:remove")
    @Log(title = "项目管理分类", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        try
        {
            Long[] arr = Convert.toLongArray(ids);
            int index=0;
            if(arr.length==1){
                //说明只选择了一条数据
                index = iKnowledgeBenchmarkingService.removeOneByID(arr[0]);
            }else{
                //说明选择了多条数据
                index =  iKnowledgeBenchmarkingService.deleteByIds(arr);

            }

            if(index==1){
                //删除成功
                return toAjax(index);
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return error("删除失败");
    }
    /*
     * 查看
     * */
    @GetMapping("/detail/{project_id}")
    public String detail(@PathVariable("project_id") Long project_id, ModelMap map){
        System.out.println("看一眼"+iKnowledgeBenchmarkingService.selectOneByID(project_id));
        map.put("KnowledgeBenchmarking",iKnowledgeBenchmarkingService.selectOneByID(project_id));
        return prefix+"/detail";
    }

    /*
    * 修改是否为标杆文件
    * */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(KnowledgeBenchmarking knowledgeBenchmarking){
        return toAjax(iKnowledgeBenchmarkingService.changeProjectStatus(knowledgeBenchmarking));
    }

    /**
     * 新增角色
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    @RequiresPermissions("cms:knowledgemanagement:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated KnowledgeBenchmarking knowledgeBenchmarking)
    {
        System.out.println(knowledgeBenchmarking);
        try {
            int index = iKnowledgeBenchmarkingService.insertProject(knowledgeBenchmarking);
            if(index==1){
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return error("失败");

    }

    /**
     * 修改项目
     * 首先根据id查询到要被修改的项目的信息
     */

    Date project_start1 = new Date();
    Date project_end1 = new Date();
    @GetMapping("/edit/{project_id}")
    public String edit(@PathVariable Long project_id, ModelMap mmap)
    {

        mmap.put("KnowledgeBenchmarking", iKnowledgeBenchmarkingService.selectOneByID(project_id));
        KnowledgeBenchmarking know = iKnowledgeBenchmarkingService.selectOneByID(project_id);
        project_start1 = know.getProject_start();
        project_end1 = know.getProject_end();


        return prefix + "/edit";
    }
    /*
    * 保存修改后的项目
    * */
    @RequiresPermissions("cms:knowledgemanagement:edit")
    @Log(title = "项目类别管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @GetMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated KnowledgeBenchmarking knowledgeBenchmarking)
    {

        if(knowledgeBenchmarking.getProject_start()==null){
            knowledgeBenchmarking.setProject_start(project_start1);
        }

        if (knowledgeBenchmarking.getProject_end()==null){
            knowledgeBenchmarking.setProject_end(project_end1);
        }
        System.out.println(project_start1);
        try {

            int index = iKnowledgeBenchmarkingService.updateProject(knowledgeBenchmarking);


            if(index==1){
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  error(e.getMessage());
        }
        return  error("服务器端消息 - 公告分类管理修改失败");
    }

}
